package com.xique.park.command.config;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.xique.park.command.handler.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 设备命令上下文
 *
 * @author WLD
 */
@Slf4j
@Component
public class CommandContext {

    private static final Map<String, Class<?>> COMMAND_CONTEXT;

    static {
        COMMAND_CONTEXT = MapUtil.<String, Class<?>>builder()
                .put("9", DevOnlineCommandHandler.class)
                .put("10", DevOfflineCommandHandler.class)
                .put("12-1", OpenGateCommandHandler.class)
                .put("12-2", CloseGateCommandHandler.class)
                .put("14", SetTimeCommandHandler.class)
                .put("16", GetTimeCommandHandler.class)
                .put("18", RebootCommandHandler.class)
                .put("19", RebootAppCommandHandler.class)
                .put("24", GetSenseCommandHandler.class)
                .put("26", RebootMonitorCommandHandler.class)
                .put("27", MarkCarInoutCommandHandler.class)
                .put("90", DownCarCommandHandler.class)
                .put("91", GetCarCommandHandler.class)
                .put("92", GetCarCntCommandHandler.class)
                .put("94", DelAllCarCommandHandler.class)
                .put("95", DownMngTypeCommandHandler.class)
                .put("96", GetMngTypeCommandHandler.class)
                .put("97", DelMngTypeCommandHandler.class)
                .put("98", DelAllMngTypeCommandHandler.class)
                .put("100", DownPassAuthCommandHandler.class)
                .put("101", GetPassAuthCommandHandler.class)
                .put("102", DelPassAuthCommandHandler.class)
                .put("103", DelAllPassAuthCommandHandler.class)
                .put("115", UpgradeCommandHandler.class)
                .put("117", DownFileCommandHandler.class)
                .put("118", GetFileCommandHandler.class)
                .put("130", SetDevParamCommandHandler.class)
                .put("134", SetLedCommandHandler.class)
                .put("135", GetLedCommandHandler.class)
                .put("136", SetLaneCommandHandler.class)
                .put("137", GetLaneCommandHandler.class)
                .put("138", SetParkCommandHandler.class)
                .put("139", GetParkCommandHandler.class)
                .put("145", SetChargeCommandHandler.class)
                .put("146", GetChargeCommandHandler.class)
                .put("148", DelAllChargeCommandHandler.class)
                .put("149", DownChargeTemplateCommandHandler.class)
                .put("152", DelAllChargeTemplateCommandHandler.class)
                .put("154", DevInitCommandHandler.class)
                .build();
    }

    public Class<?> getCommand(String cmd) {
        log.info("根据命令类型获取命令处理类，命令类型为：{}", cmd);
        if (StrUtil.isNotBlank(cmd)) {
            Class<?> command = COMMAND_CONTEXT.get(cmd);
            if (null != command) {
                return command;
            }
        }
        throw new IllegalArgumentException("命令类型错误");
    }

}
